package controllers;

import classJSON.RegResponse;
import play.mvc.*;
import models.*;
import play.data.validation.*;

/**
 * @author Andriy
 */

public class Registration extends Controller {

	public static final int REG_SUCCESS = 0;
	public static final int ERROR_LOG_EXIST = 1;

	/**
	 * Provider registration with one parameter
	 */
	public static void provSimpleRegistration(String login) {
		RegResponse regResponse = new RegResponse();
		Provs test = Provs.find("byLogin", login).first();
		if (test == null) {
			String password = Encrypt.generatepassword();
			Provs prov = new Provs(login, password);
			prov.save();
			regResponse.setPass(password);
		} else {
			regResponse.setError(ERROR_LOG_EXIST);
		}
		renderJSON(regResponse);

	}

	/**
	 * Provider registration
	 *
	 * @param login         login for application
	 *                      Personal information of Provider:
	 * @param name
	 * @param lastname
	 * @param zip
	 * @param city
	 * @param stateProvince
	 * @param country
	 * @param email         Information about service of Provider:
	 * @param typeOfCar
	 * @param seatsNumber
	 * @param fareFixed
	 * @param farePerMinute
	 */
        public static void provRegistration(@Required String login, String name, String lastname, String zip, String city,
                String stateProvince, String country, String email, String typeOfCar,
                byte seatsNumber, float fareFixed, float farePerMinute) {
                if (validation.hasErrors()) {
                        flash.keep("url");
                        flash.error("Enter your login");
                        params.flash();
                        Application.provRegistration();
                }
                Provs test = Provs.find("byLogin", login).first();
                if (test != null) {
                        flash.keep("url");
                        flash.error("Someone already has that login. Try another");
                        params.flash();
                        Application.provRegistration();
                }
                String password = Encrypt.generatepassword();
                Provs prov = new Provs(login, name, lastname, zip, city, stateProvince, country, email, typeOfCar, seatsNumber, fareFixed, farePerMinute, password);
                prov.save();
                Application.provider(prov.login, password);
        }


	/**
	 * User registration
	 * Personal information of User:
	 *
	 * @param name
	 * @param lastname
	 * @param email
	 * @param zip
	 * @param city
	 * @param stateProvince
	 * @param country
	 * @param facebook
	 * @param phone         Information about User device:
	 * @param deviceType
	 * @param deviceSn
	 */
	public static void userRegistration(@Required String deviceSn, String name, String lastname, String email, String zip, String city,
										String stateProvince, String country, String deviceType, String phone,
										String facebook) {
		if(validation.hasErrors()){
                        flash.keep("url");
                        flash.error("Enter your deviceSn");
                        params.flash();
                        Application.userRegistration();
                }
                Users test = Users.find("byDeviceSn", deviceSn).first();
		if (test != null) {
			flash.keep("url");
                        flash.error("This deviceSn already exist");
                        params.flash();
                        Application.userRegistration();
		}
		Users user = new Users(name, lastname, email, zip, city, stateProvince, country, deviceType, deviceSn, phone, facebook);
		user.save();
		Application.client(deviceSn);
	}
}
